if (typeof mainJs == 'undefined') {
    var mainJs = {};
    mainJs.autoHeight = function (s,f) {
        $(s).height($(window).height()-$("#header-navbar").height()-50);
        //tools.autoHeight({ selector:s,target:".ice-content-wrapper",adjust:(f||50)});
    }
}
if (typeof tabJs == 'undefined') {
    var tabJs = {};
    tabJs.tabs = $(".magic-tabs");
    tabJs.frameTemplate = function (code, url) {
        var id = "id_" + code;
        return '<iframe id="{0}" name="{1}" src="{2}"   height="400" class="tab-frame" frameborder="0" marginheight="0" marginwidth="0" frameborder="0" scrolling="no"  width="100%"></iframe>'
            .format( "id_" + code, "name_" + code, url);
    }
    tabJs.onShowTab=function(me, a, b, p){
        var menuTarget;
        if(!p.closeable){
            menuTarget="#index-context-menu";
        }else{
            menuTarget="#tab-context-menu";
        }
        $(a).contextmenu({
            target:menuTarget,
            onItem: function (context, e) {
                var menuValue=$(e.target).attr("value");
                if("refresh"==menuValue){
                    var tabFrame=$("#id_"+ p.code);
                    tabFrame.attr("src",tabFrame.attr("src"));
                }else if("close"==menuValue){
                    me.closeTab(p.code);
                }else if("closeOthers"==menuValue){
                    me.closeTabs(p.code);
                }else if("closeAll"==menuValue){
                    me.closeTabs();
                }
            }
        });

    // var menu;
    //    var refreshMenu={name: '刷新', fun: function () {var tabFrame=$("#id_"+ p.code);tabFrame.attr("src",tabFrame.attr("src"));}};
    //    var closeMenu={name: '关闭', img: 'http://ignitersworld.com/lab/assets/images/create.png', fun: function () {me.closeTab(p.code);}};
    //    var closeOthersMenu={name: '关闭其他', fun: function () {me.closeTabs(p.code)}};
    //    var closeAllMenu={name: '关闭所有', fun: function () {me.closeTabs()}};
    //    if(!p.closeable){
    //        menu=[refreshMenu];
    //    }else{
    //        menu=[refreshMenu,closeMenu,closeOthersMenu,closeAllMenu];
    //    }
    //    $(a).contextMenu(menu,{triggerOn:'contextmenu'});
   }

    tabJs.addTab = function (code, title,url, closeable) {
        if(!code||!title){window.console.error("the method addTab params error");return;}
        var wTab=$("div[name="+code+"]");
        if(wTab.size()>0){
            tabJs.tabs.select(code);
            //进行刷新操作
            $("#id_" + code).prop("src",url);
            return ;
        }
        var tableItem={
            code: code,
            title: title,
            height: 'auto',
            closeable: closeable,
            el: tabJs.frameTemplate(code,url)
        }
        if (typeof tabJs.tabs.addTab != "function") {
            tabJs.tabs.mac('tabs', {
                //tabWidth: 80,
                tabHeight:32,
                //speed: 48, //default 6
                //hbarHeight: 1, //default 2

                items: [tableItem]
               // ,onShowTab:tabJs.onShowTab
            }).selectFirst();
            return;
        }
        var tab = tabJs.tabs.addTab(tableItem);
        tabJs.tabs.select(code);
        return tab;
    }

}


$(function () {
    //初始化增加首页
    tabJs.addTab("index", '首页',  tools.getContextPath() + "/common/index",false);
    //ice-sidebar-wrapper
    $("a[url]", "#sidebar-nav").click(function () {
        var $this = $(this);
        var code=$this.attr("code");
        var url= $this.attr("url");
        url= tools.getContextPath()+(url&&url.startWith("/")?(url||""):"/"+url);
        var singleTab="";
        if (storage) {
            singleTab=localStorage.getItem("config-single-tab");
        }
        if(singleTab=="single-tab"){
            var wTab=$("div[name="+code+"]");
            if(wTab.size()==0) {
                $(".magic-item").addClass("magic-closeable");
                tabJs.tabs.closeTabs();
            }
            tabJs.addTab(code, $this.attr("tab-title"), url,false);
        }else{
            tabJs.addTab(code, $this.attr("tab-title"), url,true);
        }
        mainJs.autoHeight(".tab-frame");
    });
    //调整iframe高度
    mainJs.autoHeight(".tab-frame");
    $(window).resizeEnd({
        delay: 250
    }, function () {
        mainJs.autoHeight(".tab-frame");
        $(".tab-frame").each(function(){
            var self=$(this);
            if(typeof this.contentWindow.page_handlers =="object"){
                var fun=this.contentWindow.page_handlers.resize_handler;
                if(typeof fun =="function"){
                    fun.call();
                }
            }
        });
    });

})






